home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows (5th Edition) / Programming Windows, 5th ed. - Companion CD (097-0002183)(1999).iso / Chap09 / Colors1 / Colors1.c next >
Encoding:
C/C++ Source or Header  |  1998-10-09  |  9.5 KB  |  258 lines

  1. /*----------------------------------------
  2.    COLORS1.C -- Colors Using Scroll Bars
  3.                 (c) Charles Petzold, 1998
  4.   ----------------------------------------*/
  5.  
  6. #include <windows.h>
  7.  
  8. LRESULT CALLBACK WndProc    (HWND, UINT, WPARAM, LPARAM) ;
  9. LRESULT CALLBACK ScrollProc (HWND, UINT, WPARAM, LPARAM) ;
  10.  
  11. int     idFocus ;
  12. WNDPROC OldScroll[3] ;
  13.  
  14. int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
  15.                     PSTR szCmdLine, int iCmdShow)
  16. {
  17.      static TCHAR szAppName[] = TEXT ("Colors1") ;
  18.      HWND         hwnd ;
  19.      MSG          msg ;
  20.      WNDCLASS     wndclass ;
  21.      
  22.      wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  23.      wndclass.lpfnWndProc   = WndProc ;
  24.      wndclass.cbClsExtra    = 0 ;
  25.      wndclass.cbWndExtra    = 0 ;
  26.      wndclass.hInstance     = hInstance ;
  27.      wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  28.      wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  29.      wndclass.hbrBackground = CreateSolidBrush (0) ;
  30.      wndclass.lpszMenuName  = NULL ;
  31.      wndclass.lpszClassName = szAppName ;
  32.      
  33.      if (!RegisterClass (&wndclass))
  34.      {
  35.           MessageBox (NULL, TEXT ("This program requires Windows NT!"),
  36.                       szAppName, MB_ICONERROR) ;
  37.           return 0 ;
  38.      }
  39.      
  40.      hwnd = CreateWindow (szAppName, TEXT ("Color Scroll"),
  41.                           WS_OVERLAPPEDWINDOW,
  42.                           CW_USEDEFAULT, CW_USEDEFAULT,
  43.                           CW_USEDEFAULT, CW_USEDEFAULT,
  44.                           NULL, NULL, hInstance, NULL) ;
  45.      
  46.      ShowWindow (hwnd, iCmdShow) ;
  47.      UpdateWindow (hwnd) ;
  48.      
  49.      while (GetMessage (&msg, NULL, 0, 0))
  50.      {
  51.           TranslateMessage (&msg) ;
  52.           DispatchMessage  (&msg) ;
  53.      }
  54.      return msg.wParam ;
  55. }
  56.  
  57. LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  58. {
  59.      static COLORREF crPrim[3] = { RGB (255, 0, 0), RGB (0, 255, 0),
  60.                                    RGB (0, 0, 255) } ;
  61.      static HBRUSH  hBrush[3], hBrushStatic ;
  62.      static HWND    hwndScroll[3], hwndLabel[3], hwndValue[3], hwndRect ;
  63.      static int     color[3], cyChar ;
  64.      static RECT    rcColor ;
  65.      static TCHAR * szColorLabel[] = { TEXT ("Red"), TEXT ("Green"), 
  66.                                        TEXT ("Blue") } ;
  67.      HINSTANCE      hInstance ;
  68.      int            i, cxClient, cyClient ;
  69.      TCHAR          szBuffer[10] ;
  70.      
  71.      switch (message)
  72.      {
  73.      case WM_CREATE :
  74.           hInstance = (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE) ;
  75.           
  76.                // Create the white-rectangle window against which the 
  77.                // scroll bars will be positioned. The child window ID is 9.
  78.           
  79.           hwndRect = CreateWindow (TEXT ("static"), NULL,
  80.                                    WS_CHILD | WS_VISIBLE | SS_WHITERECT,
  81.                                    0, 0, 0, 0,
  82.                                    hwnd, (HMENU) 9, hInstance, NULL) ;
  83.           
  84.           for (i = 0 ; i < 3 ; i++)
  85.           {
  86.                     // The three scroll bars have IDs 0, 1, and 2, with
  87.                     // scroll bar ranges from 0 through 255.
  88.                
  89.                hwndScroll[i] = CreateWindow (TEXT ("scrollbar"), NULL,
  90.                                              WS_CHILD | WS_VISIBLE | 
  91.                                              WS_TABSTOP | SBS_VERT,
  92.                                              0, 0, 0, 0, 
  93.                                              hwnd, (HMENU) i, hInstance, NULL) ;
  94.                
  95.                SetScrollRange (hwndScroll[i], SB_CTL, 0, 255, FALSE) ;
  96.                SetScrollPos   (hwndScroll[i], SB_CTL, 0, FALSE) ;
  97.                
  98.                     // The three color-name labels have IDs 3, 4, and 5, 
  99.                     // and text strings "Red", "Green", and "Blue".
  100.                
  101.                hwndLabel [i] = CreateWindow (TEXT ("static"), szColorLabel[i],
  102.                                              WS_CHILD | WS_VISIBLE | SS_CENTER,
  103.                                              0, 0, 0, 0, 
  104.                                              hwnd, (HMENU) (i + 3), 
  105.                                              hInstance, NULL) ;
  106.                
  107.                     // The three color-value text fields have IDs 6, 7, 
  108.                     // and 8, and initial text strings of "0".
  109.                
  110.                hwndValue [i] = CreateWindow (TEXT ("static"), TEXT ("0"),
  111.                                              WS_CHILD | WS_VISIBLE | SS_CENTER,
  112.                                              0, 0, 0, 0,
  113.                                              hwnd, (HMENU) (i + 6), 
  114.                                              hInstance, NULL) ;
  115.                
  116.                OldScroll[i] = (WNDPROC) SetWindowLong (hwndScroll[i], 
  117.                                              GWL_WNDPROC, (LONG) ScrollProc) ;
  118.                
  119.                hBrush[i] = CreateSolidBrush (crPrim[i]) ;
  120.           }
  121.           
  122.           hBrushStatic = CreateSolidBrush (
  123.                               GetSysColor (COLOR_BTNHIGHLIGHT)) ;
  124.           
  125.           cyChar = HIWORD (GetDialogBaseUnits ()) ;
  126.           return 0 ;
  127.           
  128.      case WM_SIZE :
  129.           cxClient = LOWORD (lParam) ;
  130.           cyClient = HIWORD (lParam) ;
  131.           
  132.           SetRect (&rcColor, cxClient / 2, 0, cxClient, cyClient) ;
  133.           
  134.           MoveWindow (hwndRect, 0, 0, cxClient / 2, cyClient, TRUE) ;
  135.           
  136.           for (i = 0 ; i < 3 ; i++)
  137.           {
  138.                MoveWindow (hwndScroll[i],
  139.                            (2 * i + 1) * cxClient / 14, 2 * cyChar,
  140.                            cxClient / 14, cyClient - 4 * cyChar, TRUE) ;
  141.                
  142.                MoveWindow (hwndLabel[i],
  143.                            (4 * i + 1) * cxClient / 28, cyChar / 2,
  144.                            cxClient / 7, cyChar, TRUE) ;
  145.                
  146.                MoveWindow (hwndValue[i],
  147.                            (4 * i + 1) * cxClient / 28, 
  148.                            cyClient - 3 * cyChar / 2,
  149.                            cxClient / 7, cyChar, TRUE) ;
  150.           }
  151.           SetFocus (hwnd) ;
  152.           return 0 ;
  153.           
  154.      case WM_SETFOCUS :
  155.           SetFocus (hwndScroll[idFocus]) ;
  156.           return 0 ;
  157.           
  158.      case WM_VSCROLL :
  159.           i = GetWindowLong ((HWND) lParam, GWL_ID) ;
  160.           
  161.           switch (LOWORD (wParam))
  162.           {
  163.           case SB_PAGEDOWN :
  164.                color[i] += 15 ;
  165.                                              // fall through
  166.           case SB_LINEDOWN :
  167.                color[i] = min (255, color[i] + 1) ;
  168.                break ;
  169.                
  170.           case SB_PAGEUP :
  171.                color[i] -= 15 ;
  172.                                              // fall through
  173.           case SB_LINEUP :
  174.                color[i] = max (0, color[i] - 1) ;
  175.                break ;
  176.                
  177.           case SB_TOP :
  178.                color[i] = 0 ;
  179.                break ;
  180.                
  181.           case SB_BOTTOM :
  182.                color[i] = 255 ;
  183.                break ;
  184.                
  185.           case SB_THUMBPOSITION :
  186.           case SB_THUMBTRACK :
  187.                color[i] = HIWORD (wParam) ;
  188.                break ;
  189.                
  190.           default :
  191.                break ;
  192.           }
  193.           SetScrollPos  (hwndScroll[i], SB_CTL, color[i], TRUE) ;
  194.           wsprintf (szBuffer, TEXT ("%i"), color[i]) ;
  195.           SetWindowText (hwndValue[i], szBuffer) ;
  196.           
  197.           DeleteObject ((HBRUSH) 
  198.                SetClassLong (hwnd, GCL_HBRBACKGROUND, (LONG) 
  199.                     CreateSolidBrush (RGB (color[0], color[1], color[2])))) ;
  200.           
  201.           InvalidateRect (hwnd, &rcColor, TRUE) ;
  202.           return 0 ;
  203.           
  204.      case WM_CTLCOLORSCROLLBAR :
  205.           i = GetWindowLong ((HWND) lParam, GWL_ID) ;
  206.           return (LRESULT) hBrush[i] ;
  207.                
  208.      case WM_CTLCOLORSTATIC :
  209.           i = GetWindowLong ((HWND) lParam, GWL_ID) ;
  210.                
  211.           if (i >= 3 && i <= 8)    // static text controls
  212.           {
  213.                SetTextColor ((HDC) wParam, crPrim[i % 3]) ;
  214.                SetBkColor ((HDC) wParam, GetSysColor (COLOR_BTNHIGHLIGHT));
  215.                return (LRESULT) hBrushStatic ;
  216.           }
  217.           break ;
  218.                
  219.      case WM_SYSCOLORCHANGE :
  220.           DeleteObject (hBrushStatic) ;
  221.           hBrushStatic = CreateSolidBrush (GetSysColor (COLOR_BTNHIGHLIGHT)) ;
  222.           return 0 ;
  223.                
  224.      case WM_DESTROY :
  225.           DeleteObject ((HBRUSH)
  226.                SetClassLong (hwnd, GCL_HBRBACKGROUND, (LONG) 
  227.                     GetStockObject (WHITE_BRUSH))) ;
  228.                
  229.           for (i = 0 ; i < 3 ; i++)
  230.                DeleteObject (hBrush[i]) ;
  231.                
  232.           DeleteObject (hBrushStatic) ;
  233.           PostQuitMessage (0) ;
  234.           return 0 ;
  235.      }
  236.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  237. }
  238.      
  239. LRESULT CALLBACK ScrollProc (HWND hwnd, UINT message, 
  240.                              WPARAM wParam, LPARAM lParam)
  241. {
  242.      int id = GetWindowLong (hwnd, GWL_ID) ;
  243.           
  244.      switch (message)
  245.      {
  246.      case WM_KEYDOWN :
  247.           if (wParam == VK_TAB)
  248.                SetFocus (GetDlgItem (GetParent (hwnd), 
  249.                     (id + (GetKeyState (VK_SHIFT) < 0 ? 2 : 1)) % 3)) ;
  250.           break ;
  251.                
  252.      case WM_SETFOCUS :
  253.           idFocus = id ;
  254.           break ;
  255.      }
  256.      return CallWindowProc (OldScroll[id], hwnd, message, wParam, lParam) ;
  257. }
  258.